package Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;
import Entities.SendPermissionRequestEntity;
import GUI.SendPermissionRequestGUI;
import Server.RsWraper;
import Server.Tuple;

/**
 * The Class SendPermissionRequestController.
 * Controller for sending permission to customer activity.
 */
public class SendPermissionRequestController implements Validate,Observer{

	/** The spr gui. */
	private SendPermissionRequestGUI sprGUI;
	
	/** The spr entity. */
	private SendPermissionRequestEntity sprEnt;
	
	/** The previous controller. */
	private MarketingEmployeeController prev;
	
	/** The operation. */
	private int operation=0;
	
	/** The tuples. */
	private ArrayList<Tuple>tuples;
	
	/**
	 * Instantiates a new send permission request controller.
	 *
	 * @param sendPermissionRequestGui the send permission request gui
	 * @param sendPermissionRequestEntity the send permission request entity
	 */
	public SendPermissionRequestController(SendPermissionRequestGUI sendPermissionRequestGui,
			SendPermissionRequestEntity sendPermissionRequestEntity) {
		
		this.sprGUI=sendPermissionRequestGui;
		this.sprGUI.addSendRequestListener(new SendRequestListener());
		this.sprGUI.addBackListener(new BackListener());
		this.sprEnt=sendPermissionRequestEntity;
		ClientController.setObserver(this);
		addCustomersToCb();
		this.sprGUI.setVisible(true);
	}
	
	
	/**
	 * Adds the customers to the combo box.
	 */
	public void addCustomersToCb()
	{
		String query = "SELECT customers.cid,users.name "
				+ "FROM customers,users"
				+ " WHERE customers.cid = users.uid";
		try {
			ClientController.observableClient.sendToServer(query);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	/* (non-Javadoc)
	 * @see java.util.Observer#update(java.util.Observable, java.lang.Object)
	 */
	@Override
	public void update(Observable o, Object arg) {
		String query;
		String msg=(String)arg.toString();
		if(msg.equals("#Server:Action succeded"))
		{
			if(sprEnt.getAnswer().equals("NO"))
			sprGUI.displayMessage("Customer didn't give you permission");
			else
				sprGUI.displayMessage("Customer permits sending him offers for product :"+sprEnt.getPid()+","+sprEnt.getPname());
		}
		if(operation==0)
		{
			tuples=((RsWraper)arg).getRows();
			for(int i=0;i<tuples.size();i++)
				sprGUI.getCustomersCb().addItem(tuples.get(i).get("CID")+","+tuples.get(i).get("NAME"));
			operation=1;
			query="SELECT pid,name FROM products";
			try {
				ClientController.observableClient.sendToServer(query);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		else if(operation==1)
		{
			tuples=((RsWraper)arg).getRows();
			for(int i=0;i<tuples.size();i++)
				sprGUI.getProductsCb().addItem(tuples.get(i).get("PID")+","+tuples.get(i).get("NAME"));
			operation=2;
		}
		
	}

	
	/**
	 * Sets the previous.
	 *
	 * @param prev the new previous
	 */
	public void setPrev(MarketingEmployeeController prev) {
		this.prev = prev;
		
	}


	/* (non-Javadoc)
	 * @see Controllers.Validate#validateInput()
	 */
	@Override
	public boolean validateInput() {
		if(sprGUI.getCustomersCb().getSelectedItem().equals("")||sprGUI.getProductsCb().getSelectedItem().equals(""))
		return false;
		return true;
	}
	
	/**
	 * The listener interface for receiving sendRequest events.
	 * The class that is interested in processing a sendRequest
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addSendRequestListener<code> method. When
	 * the sendRequest event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @see SendRequestEvent
	 */
	class SendRequestListener implements ActionListener
	{

		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			if(validateInput())
			{
				String query,customer,product;
				String[] str;
				customer=(String) sprGUI.getCustomersCb().getSelectedItem();
				str=customer.split(",");
				sprEnt.setCid(str[0]);
				product=(String)sprGUI.getProductsCb().getSelectedItem();
				str=product.split(",");
				sprEnt.setPid(str[0]);
				sprEnt.setPname(str[1]);
				sprEnt.setAnswer("YES");
				query="INSERT INTO authorizations (`cid`, `pid`,`interestlevel`) "
					+ "VALUES ('"+sprEnt.getCid()+"' , '"+sprEnt.getPid()+"' , '1')";
				try {
					ClientController.observableClient.sendToServer(query);
				} catch (IOException e1) {
					e1.printStackTrace();
				}
				
				
				
			}
			
		}
		
	}
	
	/**
	 * The listener interface for receiving back events.
	 * The class that is interested in processing a back
	 * event implements this interface, and the object created
	 * with that class is registered with a component using the
	 * component's <code>addBackListener<code> method. When
	 * the back event occurs, that object's appropriate
	 * method is invoked.
	 *
	 * @see BackEvent
	 */
	class BackListener implements ActionListener
	{

		/* (non-Javadoc)
		 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
		 */
		@Override
		public void actionPerformed(ActionEvent e) {
			LoginController.mainWindow.setTitle("Marketing employee");
			LoginController.mainWindow.setContentPane(prev.getMarketingEmployeeGui());
			
		}
		
	}
}
